/**
 * Created by Jone on 2016/7/23.
 */

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
    function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
        $stateProvider
            .state('page', {
                url: '/page',
                abstract: true,
                templateUrl: 'pages/page.html',
                resolve: helper.resolveFor('modernizr', 'icons'),
                controller: ["$rootScope", function($rootScope) {
                    $rootScope.app.layout.isBoxed = false;
                }]
            })
            .state('page.login', {
                url: '/login',
                title: "Login",
                templateUrl: 'pages/login.html',
                controller: 'LoginFormController'
            })
            .state('page.register', {
                url: '/register',
                title: "Register",
                templateUrl: 'pages/register.html',
                controller: 'RegisterFormController',
                resolve: helper.resolveFor( 'toaster', 'ngDialog')
            })
            .state('page.recover', {
                url: '/recover',
                title: "Recover",
                templateUrl: 'pages/recover.html'
            })
            .state('page.lock', {
                url: '/lock',
                title: "Lock",
                controller: 'LockFormController',
                templateUrl: 'pages/lock.html',
                resolve: helper.resolveFor( 'toaster', 'ngDialog')
            })
            .state('page.404', {
                url: '/404',
                title: "Not Found",
                templateUrl: 'pages/404.html'
            })
    }]);


/**=========================================================
 * Module: access-login.js
 * Demo for login api
 =========================================================*/

App.controller('LoginFormController', function($scope, $rootScope, $http, $state, $cookieStore) {

    // bind here all data from the form
    if($rootScope.user != undefined) {
        $scope.account = {'email' : $rootScope.user.name};
    }
    // place the message if something goes wrong
    $scope.authMsg = '';

    $scope.login = function() {
        $scope.authMsg = '';
        if($scope.loginForm.$valid) {
            $http
                .post('/accounts/login', $scope.account)
                .success(function(response) {
                    // assumes if ok, response is an object with some data, if not, a string with error
                    // customize according to your api
                    if ( response.code != 0 ) {
                        $scope.authMsg = response.msg;
                    }else{
                        response.data.name = 'JONE';
                        response.data.job = 'ng-developer';
                        response.data.picture = 'img/user/02.jpg';
                        $cookieStore.put('user', response.data);
                        $rootScope.user = response.data;
                        $state.go('app.dashboard');
                    }
                })
                .error(function(x) {
                    $scope.authMsg = 'Server Request Error';
                });
        }
        else {
            // set as dirty if the user click directly to login so we show the validation messages
            /*$scope.loginForm.account_email.$dirty = true;
            $scope.loginForm.account_password.$dirty = true;*/
        }
    };

});

/**=========================================================
 * Module: access-register.js
 * Demo for register account api
 =========================================================*/

App.controller('RegisterFormController', function($scope, $rootScope, $http, $state, $timeout, toaster) {

    // bind here all data from the form
    $scope.account = {};
    // place the message if something goes wrong
    $scope.authMsg = '';

    $scope.register = function() {
        $scope.authMsg = '';
        if($scope.registerForm.$valid) {
            $http
                .post('/accounts/register', $scope.account)
                .success(function(response) {
                    // assumes if ok, response is an object with some data, if not, a string with error
                    // customize according to your api
                    if ( response.code !=  0 ) {
                        $scope.authMsg = response.msg;
                    }else{
                        toaster.pop('success', '提示', '注册成功');
                        $rootScope.user.name = $scope.account.email;
                        $timeout(function(){$state.go('page.login')}, 2000 );
                    }
                })
                .error(function(x) {
                    $scope.authMsg = 'Server Request Error';
                });
        }
        else {
            // set as dirty if the user click directly to login so we show the validation messages
            $scope.registerForm.account_email.$dirty = true;
            $scope.registerForm.account_password.$dirty = true;
            $scope.registerForm.account_agreed.$dirty = true;

        }
    };

});

/**=========================================================
 * Module: access-register.js
 * Demo for register account api
 =========================================================*/

App.controller('LockFormController', function($scope, $rootScope, $cookieStore, $http, $state, $timeout, toaster) {

    // bind here all data from the form
    $scope.lock = {};

    // place the message if something goes wrong
    $scope.authMsg = '';

    $scope.unlock = function() {
        $scope.authMsg = '';
        if($scope.lockForm.$valid) {
            $http
                .post('/accounts/login', {email : $cookieStore.get('user').email, password : $scope.lock.password})
                .success(function(response) {
                    // assumes if ok, response is an object with some data, if not, a string with error
                    // customize according to your api
                    if ( response.code !=  0 ) {
                        $scope.authMsg = response.msg;
                    }else{
                        $cookieStore.put('user', response.data);
                        $rootScope.user.name = response.data.email;
                        $state.go('app.dashboard')
                    }
                })
                .error(function(x) {
                    $scope.authMsg = 'Server Request Error';
                });
        }
        else {
            // set as dirty if the user click directly to login so we show the validation messages
            $scope.lockForm.lock_password.$dirty = true;
        }
    };

});